import time

from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.system.models import Permission
from apps.system.serializers.permission import PermissionSerializer
from apps.utils.menu import tree_menu
from conf.config import METHOD_LIST


class PermissionView(ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer

    pagination_class = None  # 禁用分页

    def list(self, request, *args, **kwargs):
        fields = ['id', 'name', 'parent', 'type', 'level', 'codes', 'urls', 'method_types', 'is_active']  # 只获取这些字段
        queryset = self.filter_queryset(self.get_queryset()).values(*fields)
        # queryset = queryset.filter(type=10).values(*fields)
        if METHOD_LIST[0] != '无':
            METHOD_LIST.insert(0, '无')

        data = {
            'items': tree_menu(queryset),
            'type_list': Permission.menu_type_choices,  # 获取Permission 里边得menu_type_choices
            'method_list': METHOD_LIST
        }

        return Response(data, status=status.HTTP_200_OK)





